package com.aptech.action;

import com.aptech.business.CustomerBusiness;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteCustomersAction extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String[] ids = request.getParameterValues("check");

            if (ids == null) {
                request.setAttribute("customer_err", "No item has been checked...");
                request.getRequestDispatcher("/getCustomerList").forward(request, response);
                return;
            }
            String deletedItems = CustomerBusiness.deleteCustomers(ids);
            request.setAttribute("customer_msg", "You have deleted customers with ID: (" + deletedItems.trim() + ")");
            request.getRequestDispatcher("/getCustomerList").forward(request, response);


        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            Logger.getLogger(DeleteCustomersAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
            Logger.getLogger(DeleteCustomersAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            ex.printStackTrace();
            Logger.getLogger(DeleteCustomersAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
            Logger.getLogger(DeleteCustomersAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
